package org.newcih.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Range<E> implements Serializable {

    private static final long serialVersionUID = -6032778798286384476L;

    private String field;
    private Comparable from;
    private Comparable to;
    private Boolean includeNull;

    public Range(String field) {
        this.field = field;
    }

    public Range(String field, Comparable from, Comparable to) {
        this.field = field;
        this.from = from;
        this.to = to;
    }

    public Range(Range<E> other) {
        this.field = other.field;
        this.from = other.from;
        this.to = other.to;
        this.includeNull = other.includeNull;
    }

    public boolean isFromSet() {
        return getFrom() != null;
    }

    public boolean isToSet() {
        return getTo() != null;
    }

    public boolean isIncludeNullSet() {
        return includeNull != null;
    }

    public boolean isBetween() {
        return isFromSet() && isToSet();
    }

    public boolean isSet() {
        return isFromSet() || isToSet() || isIncludeNullSet();
    }

    public boolean isValid() {
        return !isBetween() || getFrom().compareTo(getTo()) <= 0;
    }

}
